﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ajax.BLL;
using System.Text;

namespace Test.Controllers
{
	public class ProductsController : Controller
	{
		//
		// GET: /Products/

		public ActionResult Index()
		{
			ProductClass products = new ProductClass();
			List<Ajax.Model.ProductClass> list = new List<Ajax.Model.ProductClass>();
			list = products.GetModelList("");
			string header = @"<h3 class='menuheader expandable' style='color: #006FA1; '>{0}</h3>";
			StringBuilder result = new StringBuilder();

			foreach (var pItem in list.Where(t => t.PID == null))
			{
				result.Append(string.Format(header, pItem.Name));
				// classDetail
				result.Append("<ul class='categoryitems'>");
				string detailHtml = @"<li style='cursor:pointer'><a tag = '1' classID='{0}' href='#'>{1}</a></li>";
				foreach (var item in list.Where(t => t.PID == pItem.ID))
				{
					result.Append(string.Format(detailHtml, item.ID, item.Name));
				}
				result.Append("</ul>");
			}
			ViewData["class"] = result.ToString();

			return View();
		}
		/// <summary>
		/// 获取该分类下的所有产品
		/// </summary>
		/// <param name="classID">产品分类ID</param>
		/// <returns></returns>
		public JsonResult GetAllProductByClass(int classID)
		{
			Products products = new Products();
			List<Ajax.Model.Products> list = new List<Ajax.Model.Products>();
			list = products.GetModelList(" ClassID = " + classID);
			return Json(list, JsonRequestBehavior.AllowGet);
		}

		public JsonResult GetProductDesciption(int productID)
		{
			Products products = new Products();
			Ajax.Model.Products p = new Ajax.Model.Products();
			p = products.GetModel(productID);
			if (p == null)
			{
				return Json(new { name = "", desc = "" }, JsonRequestBehavior.AllowGet);
			}
			return Json(new { name = p.ProductName, desc = p.Description }, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// get all products class 
		/// </summary>
		/// <returns></returns>
		public JsonResult GetAllClass()
		{
			ProductClass products = new ProductClass();
			List<Ajax.Model.ProductClass> list = new List<Ajax.Model.ProductClass>();
			list = products.GetModelList("");
			//<h3 class="menuheader expandable" style="color: #006FA1; background-image: url('../../Content/Images/moretitle.png')));">
			//        Technology Transmitting For Concrete Admixtures </h3>
			//    <ul class="categoryitems">
			//        <li><a href="#">Horizontal CSS Menus</a></li>
			//        <li>
			//            <a href="#">Vertical CSS Menus</a></li>
			//        <li>
			//            <a href="#">Image CSS</a></li>
			//    </ul>
			string header = @"<h3 class='menuheader expandable' style='color: #006FA1; background-image: url('../../Content/Images/moretitle.png')));'>{0}</h3>";
			StringBuilder result = new StringBuilder();

			foreach (var pItem in list.Where(t => t.PID == 0))
			{
				result.Append(string.Format(header, pItem.Name));
				// classDetail
				result.Append("<ul class='categoryitems'>");
				string detailHtml = @"<li><a tag = '1' classID='{1}' href='#'>{1}</a></li>";
				foreach (var item in list.Where(t => t.PID == pItem.ID))
				{
					result.Append(string.Format(detailHtml, item.ID, item.Name));
				}
				result.Append("</ul>");
			}

			ViewData["class"] = result.ToString();

			return Json(list, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		///  get product message by id 
		/// </summary>
		/// <param name="id">Product id </param>
		/// <returns>product model</returns>
		public JsonResult GetProductByID(int id)
		{
			Products product = new Products();
			var pro = product.GetModel(id);
			return Json(pro, JsonRequestBehavior.AllowGet);
		}
	}
}
